package crack.ch1;

/**
 * @author Pavel Belevich
 *         Date: 6/30/12
 *         Time: 6:13 PM
 */
public class Problem6 {

    static void rotate(int[][] matrix, int n) {
        for (int layer = 0 ; layer < n/2 ; layer++) {
            int first = layer;
            int last = n - layer - 1;
            for (int i = first ; i < last ; i++) {
                int offset = i - first;

                int temp = matrix[first][i];
                matrix[first][i] = matrix[last - offset][first];
                matrix[last - offset][first] = matrix[last][last - offset];
                matrix[last][last - offset] = matrix[i][last];
                matrix[i][last] = temp;
            }
        }
    }

    public static void main(String[] args) {
        int M = 11;
        int[][] matrix = new int[M][M];
        int k = 0;
        for (int i = 0 ; i < matrix.length ; i++) {
            for (int j = 0 ; j < matrix.length ; j++) {
                matrix[i][j] = k++;
                System.out.print(matrix[i][j] + "\t");
            }
            System.out.println();
        }
        rotate(matrix, M);
        for (int i = 0 ; i < matrix.length ; i++) {
            for (int j = 0 ; j < matrix.length ; j++) {
                System.out.print(matrix[i][j] + "\t");
            }
            System.out.println();
        }
    }

}
